package fpc.controls.text;
import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import fpc.AComponent;
import fpc.containers.Container;
import fpc.IAutoSizable;
import fpc.ALayoutable;
import fpc.styles.ExtendedTextFormat;
import fpc.styles.IStylized;
import fpc.styles.skins.SkinType;
import fpc.styles.skins.TextFormatType;
import fpc.styles.Style;
import fpc.utils.ClassFactory;
import fpc.utils.FramePostponer;
import fpc.utils.Herald;
import fpc.utils.Maths;

class Label extends AComponent, implements IAutoSizable
{
	var _tf:TextField;
	
	public function new(text:String = "")
	{
		_textFormatType = TEXT_FORMAT_LABEL;
		
		_autoSize = true;
		
		super();
		
		this.text = text;
	}
	
	override function createBaseObject():DisplayObject
	{
		_tf = new TextField();
		_tf.selectable = false;
		_tf.autoSize = TextFieldAutoSize.LEFT;
		return _tf;
	}
	
	// PROTECTED METHODS
	
	function updateSize()
	{
		_tf.width = _width;
		_tf.height = _height;
		if (_autoSize)
		{
			_width = Std.int(_tf.width);
			_height = Std.int(_tf.height);
		}
		else
		{
			if (_width < _tf.width)
			{
				_width = _tf.width;
			}
			if (_height < _tf.height)
			{
				_height = _tf.height;
			}
		}
		heraldResize.dispatch(this);
	}
	
	// HANDLERS
	
	// STYLE
	
	var _activeTextFormat:ExtendedTextFormat;
	
	override function updateStyle()
	{
		if (_textFormat != null)
		{
			applyTextFormat(_textFormat);
		}
		else
		{
			var textFormat = Style.findTextFormat(this, _textFormatType);
			if (textFormat != _activeTextFormat)
			{
				applyTextFormat(textFormat);
			}
		}
	}
	
	public var textFormatType(getTextFormatType, setTextFormatType):TextFormatType;
	var _textFormatType:TextFormatType;
	function getTextFormatType()
	{
		return _textFormatType;
	}
	function setTextFormatType(value)
	{
		_textFormatType = value;
		updateStyle();
		return _textFormatType;
	}
	
	function applyTextFormat(value:ExtendedTextFormat)
	{
		_activeTextFormat = value;
		ExtendedTextFormat.clearTextFormat(_tf);
		if (value != null)
		{
			value.applyTo(_tf);
		}
	}
	
	// PROPERTIES
	
	public var text(getText, setText):String;
	function setText(value:String):String
	{
		_tf.text = value != null ? value : '';
		updateSize();
		return value;
	}
	function getText():String
	{
		return _tf.text;
	}
	
	public var htmlText(getHtmlText, setHtmlText):String;
	function getHtmlText()
	{
		return _tf.htmlText;
	}
	function setHtmlText(value)
	{
		_tf.htmlText = value;
		return value;
	}
	
	public function getXMLText(?beginIndex:Int, ?endIndex:Int)
	{
		return _tf.getXMLText(beginIndex, endIndex);
	}
	
	function insertXMLText(beginIndex:Int, endIndex:Int, richText:String, ?pasting:Bool)
	{
		_tf.insertXMLText(beginIndex, endIndex, richText, pasting);
	}
	
	public var textFormat(getTextFormat, setTextFormat):ExtendedTextFormat;
	var _textFormat:ExtendedTextFormat;
	function getTextFormat()
	{
		return _textFormat;
	}
	function setTextFormat(value)
	{
		_textFormat = value;
		if (_activeTextFormat != value)
		{
			applyTextFormat(value);
		}
		return _textFormat;
	}
	
	public var wordWrap(getWordWrap, setWordWrap):Bool;
	function getWordWrap()
	{
		return _tf.wordWrap;
	}
	function setWordWrap(value)
	{
		_tf.wordWrap = value;
		return value;
	}
	
	override function getVisible()
	{
		return _tf.visible;
	}
	override function setVisible(value)
	{
		return _tf.visible = value;
	}
	
	public var selectable(getSelectable, setSelectable):Bool;
	function getSelectable()
	{
		return _tf.selectable;
	}
	function setSelectable(value)
	{
		_tf.selectable = value;
		return value;
	}
	
	// LAYOUT
	
	public var autoSize(getAutoSize, setAutoSize):Bool;
	var _autoSize:Bool;
	function setAutoSize(value:Bool):Bool
	{
		_autoSize = value;
		FramePostponer.addResize(updateSize);
		return _autoSize;
	}
	function getAutoSize():Bool
	{
		return _autoSize;
	}
	
	override function setWidth(value)
	{
		var oldWidth = _width;
		_width = value;
		if (Math.abs(oldWidth - value) > .5)
		{
			updateSize();
		}
		return value;
	}
	
	override function setHeight(value)
	{
		var oldHeight = _width;
		_height = value;
		if (Math.abs(oldHeight - value) > .5)
		{
			updateSize();
		}
		return value;
	}
	
	public static var TEXT_FORMAT_LABEL = new TextFormatType(
		new ExtendedTextFormat('Tahoma')
	);
}
